using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;

namespace GT.BlogBox.Features.BlogEnhancements
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("2b5404ae-6bfb-44d9-bcba-f88ec8cf769c")]
    public class BlogEnhancementsEventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb web = (SPWeb)properties.Feature.Parent;

            string masterUrl = "gtblog.master";
            masterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "_catalogs/masterpage/" + masterUrl);

            if (!web.GetFile(masterUrl).Exists) throw new SPException("'gtblog.master' does not exist. Active feature on root site.");

            web.MasterUrl = masterUrl;
            web.CustomMasterUrl = masterUrl;
            web.Update();

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPWeb web = (SPWeb)properties.Feature.Parent;

            string masterUrl = "V4.master";
            masterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "_catalogs/masterpage/" + masterUrl);

            if (!web.GetFile(masterUrl).Exists) throw new SPException("V4.master-file does not exist. Contact SiteAdministrator.");

            web.MasterUrl = masterUrl;
            web.CustomMasterUrl = masterUrl;
            web.Update();

        }

    }
}
